//
//  MoveByForward.cpp
//  client
//
//  Created by DuanHongbo on 15/1/29.
//
//

#include "MoveByForward.h"


MoveByForward::MoveByForward()
:m_autoHeadOn(false),
m_initAngle(0) {
}

MoveByForward::~MoveByForward() {
}

bool MoveByForward::initWithDuration(float duration, const Vec3& deltaPosition, bool autoHeadOn, float initAngle) {
    if (MoveBy::initWithDuration(duration, deltaPosition)) {
        m_autoHeadOn = autoHeadOn;
        m_initAngle = initAngle;
        return true;
    }
    
    return false;
}

MoveByForward* MoveByForward::create(float duration, const Vec2& deltaPosition,  bool autoHeadOn, float initAngle) {
    MoveByForward* j = new MoveByForward();
    if(j->initWithDuration(duration, Vec3(deltaPosition.x,deltaPosition.y,0), autoHeadOn, initAngle)) {
        return (MoveByForward*)j->autorelease();
    }
    
    j->release();
    return j;
}

MoveByForward* MoveByForward::clone() const{
   	auto a = new (std::nothrow) MoveByForward();
    a->initWithDuration(_duration, _positionDelta,m_autoHeadOn,m_initAngle);
    a->autorelease();
    return a;
}

void MoveByForward::update(float time) {
    if (_target)
    {
        Vec3 oldPos = _previousPosition;
        MoveBy::update(time);
        Vec3 newPos = _target->getPosition3D();
        
        // auto head on
        if(m_autoHeadOn) {
            Vec3 v = newPos-oldPos;
            Vec2 v2 = Vec2(v.x,v.y);
            float r = v2.getAngle();
            float d = -CC_RADIANS_TO_DEGREES(r);
            d += m_initAngle;
            _target->setRotation(d);
        }
    }
}

MoveByForward* MoveByForward::reverse() const {
    Vec2 v = Vec2(_positionDelta.x,_positionDelta.y);
    return MoveByForward::create(getDuration(), -v, m_autoHeadOn, 180 - m_initAngle);
}
